package com.uni.sofia.reader.magazine.test
{
	import com.uni.sofia.reader.magazine.Magazine;
	import com.uni.sofia.reader.magazine.Page;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNull;
	import org.flexunit.asserts.assertTrue;

	public class MagazineTest
	{		
		private var issue:Magazine;
		
		[Before]
		public function setUp():void
		{
			issue = new Magazine();	
		}
		
		[Test]
		public function testGetPage():void
		{
			var p:Page = new Page(13,"testPage", "time.jpg");
			issue.addPage(p);
			
			assertTrue(p.equals(issue.getPage(13)));
			assertTrue(issue.getPage(13).equals(p));
			
			//there is no page 2
			assertNull(issue.getPage(2));
		}
		
		[Test]
		public function testNumberOfPages():void
		{
			assertEquals(0, issue.numberOfPages);
			
			issue.addPage(new Page(32, "Advertisment", "time.jpg"));
				
		    assertEquals(1, issue.numberOfPages);
			
			issue.addPage(new Page(32, "Advertisment", "time.jpg"));
			
			assertEquals(1, issue.numberOfPages);
			
			issue.addPage(new Page(25, "Advertisment", "time.jpg"));
			
			assertEquals(2, issue.numberOfPages);
		}
		
		[Test]
		public function testAddInvalidPage():void
		{
			assertNull(issue.addPage(null));
		}
		
		[Test]
		public function testAddDuplicatedPage():void
		{
		 	assertNull(issue.addPage(null));
			
			var p:Page = issue.addPage(new Page(32, "Advertisment", "time.jpg"));
			assertEquals(p, issue.getPage(32));
			
			assertNull(issue.addPage(new Page(32, "Advertisment", "time.jpg")));
		}
		
		[Test]
		public function testSortMagazine():void
		{
			var magazine:Magazine = new Magazine();
			
			var p:Page = new Page(6, "six", "assda")
			magazine.addPage(p);
			magazine.addPage(new Page(5, "five", "assda"));
			magazine.addPage(new Page(4, "four", "assda"));
			magazine.addPage(new Page(1, "one", "assda"));
			
			assertEquals(magazine.pages.getItemAt(0), p);
			magazine.sortPages();
			assertEquals(magazine.pages.getItemAt(0), magazine.coverPage); 
		}
		
	}
}